package com.distribute.framework.core.lsf;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;

import com.distribute.framework.core.lsf.entity.AppInfo;
import com.distribute.framework.core.lsf.protocol.ILsfManager;
import com.distribute.framework.core.web.helper.Utils;
import com.distribute.framework.core.web.helper.SpringBeanHelper;

public class Lsf implements InitializingBean,FactoryBean{
	AppInfo serverServerInfo=null;	
	public static Map<String,Lsf> lsfes = new HashMap();
	
	public Lsf() {
	}
	public static Lsf getBean(String beanName){
		return Utils.getBean(beanName);
	}
	public void afterPropertiesSet() throws Exception {
		//如果提供了服务，则启动提供者线程
		if(!Utils.empty(this.getUrl())){
			serverServerInfo = new AppInfo(
				this.getTitle(),
				this.getUrl(),
				UUID.randomUUID().toString(),
				new Date());			
		}
		Utils.trace("<lsf> - ["+this.getUrl()+"] 客户端初始化...");
		lsfes.put(this.getFlag(), this);
	}
		
	LsfCustomer lsfCustomer = null;
	public LsfCustomer getCustomer(){
		if(lsfCustomer==null){
			String beanName = this.getFlag()+":"+LsfCustomer.class.getName();
			BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(LsfCustomer.class);
			beanDefinitionBuilder.addPropertyReference("lsf",getLsfBeanName());
			beanDefinitionBuilder.setScope("prototype");
			SpringBeanHelper.registerBean(beanName, beanDefinitionBuilder.getBeanDefinition() );
			lsfCustomer = Utils.getBean(beanName);
		}
		return lsfCustomer; 
	}
	
	public static Lsf getLsf() {
		Lsf lsf = lsfes.get("lsf");
		if(lsf==null)return null;
		return lsf;
	}
	
	public static void initLsf() {
		for(Map.Entry<String, Lsf> entry:lsfes.entrySet()){
			entry.getValue().getProvider();
		}
	}
	
	
	LsfProvider lsfProvider = null;
	public synchronized LsfProvider getProvider() {
		if(lsfProvider==null && serverServerInfo!=null){
			String beanName = this.getFlag()+":"+LsfProvider.class.getName();
			BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(LsfProvider.class);
			beanDefinitionBuilder.addPropertyReference("lsf",getLsfBeanName());
			beanDefinitionBuilder.setScope("prototype");
			SpringBeanHelper.registerBean(beanName, beanDefinitionBuilder.getBeanDefinition() );
			lsfProvider = Utils.getBean(beanName);
		}
		return lsfProvider;
	}
	
	/**
	 * 获取当前lsf的bean name;
	 * @return
	 */
	private String getLsfBeanName() {
		return this.getFlag();
	}
	
	
	Set<String> beans = new HashSet();//本地开放的服务接口列表
	public Set<String> getBeans() {
		return beans;
	}
	public void setBeans(Set<String> beans) {
		this.beans = beans;
	}
	public void addBeans(Set<String> beanNames) {
		for(String beanName:beanNames)
			this.beans.add(beanName);
	}
	public void addBeans(String[] beanNames) {
		for(String beanName:beanNames)
			this.beans.add(beanName);
	}
	public void addBean(String beanName) {
		this.beans.add(beanName);
	}
	
	String flag;
	String redis;
	String url;//本地服务地址
	String title;//当前应用标题
	
	public String getFlag() {
		return flag;
	}
	public void setFlag(String flag) {
		this.flag = flag;
	}
	public String getRedis() {
		return redis;
	}
	public void setRedis(String redis) {
		this.redis = redis;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	
	public Object getObject() throws Exception {
		return this;
	}
	public Class getObjectType() {
		return Lsf.class;
	}
	public boolean isSingleton() {
		return false;
	}

	public AppInfo getServerServerInfo() {
		return serverServerInfo;
	}

	public void setServerServerInfo(AppInfo serverServerInfo) {
		this.serverServerInfo = serverServerInfo;
	}
	
	public static void main(String...strings){
		try {
			Lsf.getBean("data.lsf").getCustomer().invoker("com.ccclubs.terminal.remote.service.IRemoteService", "dealRemoteWithBytes", 1l,"abcdefg", new byte[]{});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	
	/**
	 * 根据接口类获取远程接口
	 * @param <T>
	 * @param cls
	 * @return
	 */
	public <T> T getService(Class<T> cls){
		String beanName = getLsfBeanName()+":"+cls.getName();
		T service = (T) Utils.getBean(beanName);
		if(service==null){
			BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(LsfServiceProxy.class);
			beanDefinitionBuilder.addPropertyReference("lsf",getLsfBeanName());
			beanDefinitionBuilder.addPropertyValue("serviceInterface", cls.getName());
			beanDefinitionBuilder.setScope("prototype");
			SpringBeanHelper.registerBean(beanName, beanDefinitionBuilder.getBeanDefinition() );
			service = (T) Utils.getBean(beanName);
		}
		return service;
	}
	
}